/**
 * Author: dnj
 * Date: August 29, 2008
 * 6.005 Elements of Software Construction
 * (c) 2008, MIT and Daniel Jackson
 * 
 * Modified Version 
 * Author: rendy.jr
 * Date: Jun 10, 2011
 * 6.005 Elements of Software Construction
 * Rendy Bambang Junior
 * rendy.jr@gmail.com
 * 
 */
package piano;
import java.applet.Applet;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.sound.midi.MidiUnavailableException;

import piano.ActionElement.ActionType;

import midi.Instrument;
import midi.Midi;
import music.Pitch;

/**
 * A skeletal applet that shows how to bind methods to key events
 */
public class PianoApplet extends Applet {
	
	public void init() {
		final Piano piano = new Piano();
		final Recorder recorder = new Recorder();
		setBackground(Color.blue);
		// this is a standard pattern for associating method calls with GUI events
		// the call to the constructor of KeyAdapter creates an object of an
		// anonymous subclass of KeyAdapter, whose keyPressed method is called
		// when a key is pressed in the GUI
		
		addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {	
				char key = (char) e.getKeyCode();
				switch(key){
				case 'R':{
					break;}
				case 'P':{
					System.out.println(recorder.getActions());
					break;}
				default:{
					piano.play(key);
					if(recorder.isRecording())
						recorder.addAction(ActionType.ADD, key);
					
					break;}
				}
			}
		});

		addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent e) {
				char key = (char) e.getKeyCode();
				switch(key){
				case 'R':{
					if(!recorder.isRecording()){
						setBackground(Color.red);
						recorder.startRecording();
					}
					else{
						setBackground(Color.blue);
						recorder.stopRecording();
					}
					break;}
				case 'P':{
					if(!recorder.isPlaying())
						recorder.startPlaying();
					else
						recorder.stopPlaying();
					break;}
				case 'I':
					piano.nextInstrument();
					break;
				default:{
					piano.stop(key);
					if(recorder.isRecording())
						recorder.addAction(ActionType.REMOVE, key);
					break;}
				}
			}
		});
	}
}